---@class RedRob.Pawn : RedRob.Actor
local Pawn = RedRob.CreateClass("Pawn","Actor")

function Pawn:BaseCreate()
    self.enableCollider = true
    ---@type GameObject
    self.gameObject = GameObject.New("pawn")
    ---@type Transform
    self.transform = self.gameObject.transform
    ---@type BoxCollider
    self.boxCollider = self.gameObject:AddComponent(TBoxCollider)
    RedRob.SceneMgr.AddChild(self.transform)
    ---@type RedRob.AiBase
    self.ai = nil
    self.isActive = true
    self.isObjectShow = true
    self.sortingOrder = -1
    self.orderStep = 10 --步进值
    self.attrList = {}
    RedRob.Actor.BaseCreate(self)
    self.sortingOrderChangeCallbackList = { }
end

function Pawn:BaseDestroy()
    RedRob.Actor.BaseDestroy(self)
    if self.gameObject then
        destroy(self.gameObject)
    end
    if self.ai then
        RedRob.AiMgr.AiToPool(self.ai)
    end
    RedRob.BufferMgr.RemoveBuff(self.id)
    self.ai = nil
    self.gameObject = nil
    self.transform = nil
    self.boxCollider = nil
    self.attrList = nil
    self.sortingOrderChangeCallbackList = nil
end
function Pawn:GetTran()
    return self.transform
end
function Pawn:SetActive(isActive)
    if self.isActive ~= isActive and self.gameObject then
        self.gameObject:SetActive(isActive)
        self.isObjectShow = isActive
    end
    self.isActive = isActive
end
--只移动位置不隐藏了
function Pawn:ShowObject(isShow)
    if self.isObjectShow ~= isShow and self.gameObject then
        if isShow then
            Util.Lua_SetLocalPositionXYZ(self.gameObject,self.pos.x,self.pos.y,self.pos.z)
        else
            Util.Lua_SetLocalPositionXYZ(self.gameObject,10000,10000,0)
        end

    end
    self.isObjectShow = isShow
end
function Pawn:EnableCollider(enable)
    self.enableCollider = enable
    self.boxCollider.enabled = enable
end
function Pawn:SetPos(x,y)
    RedRob.Actor.SetPos(self,x,y)
    Util.Lua_SetLocalPositionXYZ(self.transform,self.pos.x,self.pos.y,self.pos.z)
    FireEvent(RedRob.Config.Event.ActorPosChanged,self)
end
function Pawn:SetAi(ai)
    self.ai = ai
end
function Pawn:RegisterOrderChanged(callback,tb)
    if not self.sortingOrderChangeCallbackList then return end
    for k,v in pairs(self.sortingOrderChangeCallbackList) do
        if v.callback == callback and v.tb == tb then
            return
        end
    end
    table.insert(self.sortingOrderChangeCallbackList,{callback = callback,tb = tb})
end
function Pawn:UnRegisterOrderChanged(callback,tb)
    if not self.sortingOrderChangeCallbackList then return end
    for k,v in pairs(self.sortingOrderChangeCallbackList) do
        if v.callback == callback and v.tb == tb then
            table.remove(self.sortingOrderChangeCallbackList,k)
            return
        end
    end
end
function Pawn:SetSortingOrder(order)
    if self.sortingOrder == order then
        return false
    end
    self.sortingOrder = order
    local order1 = self:GetSortingOrder()
    for k,v in pairs(self.sortingOrderChangeCallbackList) do
        v.callback(v.tb,order1)
    end
    return true
end
function Pawn:AddBuff(buffId,overlay,during)
    local mgr = RedRob.BufferMgr
    local buff = mgr.GetBuff(self.id,buffId)
    if not buff then
        mgr.CreateBuff(buffId,overlay,self.id,during)
    else
        buff:OnBuffRefresh(overlay)
    end
end
function Pawn:RemoveBuff(buffId)
    RedRob.BufferMgr.RemoveBuff(self.id,buffId)
end
function Pawn:ClearBuff()
    RedRob.BufferMgr.RemoveBuff(self.id)
end
function Pawn:GetSortingOrder()
    return self.sortingOrder * self.orderStep
end
---@return RedRob.Buffer[]
function Pawn:GetBuffList()
    return RedRob.BufferMgr.GetBuffList(self.id)
end
function Pawn:GetAttr(attrId)
     return self.attrList[attrId] or 0
end
function Pawn:SetAttr(attrId,value)
    self.attrList[attrId] = value
end
function Pawn:Print(...)
    RedRob.ReportMgr.Print("id:"..self:GetId(),"name:"..self:GetName(),...)
end
function Pawn:PrintTrace(...)
    RedRob.ReportMgr.PrintTrace("id:"..self:GetId(),"name:"..self:GetName(),...)
end
